package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;

/**
 *
 * 下发远程复播指令
 * Created by lilongfei on 2018/7/30.
 */
public class JT_9201 implements JT808MsgBody {

    //服务器ip长度
    byte serverIpLength;
    //服务器ip
    String serverIp;
    //服务器tcp端口号
    short serverTcpPort;
    //服务器udp端口号
    short serverUdpPort;
    //监控逻辑通道号
    byte channel;
    //音视频数据类型 0 音视频 1 音频 2视频 3音频或视频
    byte type;
    //码流类型 0 主码流 ,1 子码流
    byte stream;
    //远程存储位置 0 全部 1 主存储器 2 灾备存储器
    byte local;
    //回放方式 0 正常 1 快进 2 关键帧快退  3 关键帧播放 4 单帧上传
    byte backType;
    //快进或快退倍数 回放类型为1 2 时,该字段有效,否则默认0   0 无效  1 1倍 2 2倍
    // 3 4倍 4 8倍 5 16倍
    byte multiple;
    //播放开始时间
    String  startTime;
    //播放结束时间
    String endTime;

    public JT_9201(int serverLenght,String serverIp,short serverTcpPort,short serverUdpPort,int channel,int type,int stream,
                   int local,int backType,int multiple,String startTime,String endTime){
        setIpLength(serverLenght);
        setServerIp(serverIp);
        setServerTcpPort(serverTcpPort);
        setServerUdpPort(serverUdpPort);
        setChannel(channel);
        setType(type);
        setStream(stream);
        setLocal(local);
        setBackType(backType);
        setMultiple(multiple);
        setStartTime(startTime);
        setEndTime(endTime);

    }


    /**
     * 转换字段ip长度为byte类型,保存在消息体内
     * @param lenght
     */
    public void setIpLength(int lenght){
        byte[] b_lenght = BitConverter.GetBytes(lenght);
        //此处取低8位数据进行传输
        setServerIpLength(b_lenght[3]);
    }

    /**
     * 转换数字型通道号为byte对象
     * @param channel
     */
    public void setChannel(int channel){
        byte[] b_lenght = BitConverter.GetBytes(channel);
        //此处取低8位数据进行传输
        setChannel(b_lenght[3]);
    }

    /**
     * 转换数字型类型为byte对象
     * @param type
     */
    public void setType(int type){
        byte[] b_lenght = BitConverter.GetBytes(type);
        //此处取低8位数据进行传输
        setType(b_lenght[3]);
    }

    /**
     * 转换数字型码流为byte对象
     * @param stream
     */
    public void setStream(int stream){
        byte[] b_lenght = BitConverter.GetBytes(stream);
        //此处取低8位数据进行传输
        setStream(b_lenght[3]);
    }

    /**
     * 设置远程存储位置
     * @param local
     */
    public void setLocal(int local) {
        byte[] locals = BitConverter.GetBytes(local);
        setLocal(locals[3]);
    }

    /**
     * 设定复播返回类型
     * @param backType
     */
    public void setBackType(int backType) {
        byte[] backTypes = BitConverter.GetBytes(backType);
        setBackType(backTypes[3]);
    }

    public void setMultiple(int multiple){
        byte[] multiples = BitConverter.GetBytes(multiple);
        setMultiple(multiples[3]);
    }

    public byte getServerIpLength() {
        return serverIpLength;
    }

    public void setServerIpLength(byte serverIpLenght) {
        this.serverIpLength = serverIpLenght;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public short getServerTcpPort() {
        return serverTcpPort;
    }

    public void setServerTcpPort(short serverTcpPort) {
        this.serverTcpPort = serverTcpPort;
    }

    public short getServerUdpPort() {
        return serverUdpPort;
    }

    public void setServerUdpPort(short serverUdpPort) {
        this.serverUdpPort = serverUdpPort;
    }

    public byte getChannel() {
        return channel;
    }

    public void setChannel(byte channel) {
        this.channel = channel;
    }

    public byte getType() {
        return type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public byte getStream() {
        return stream;
    }

    public void setStream(byte stream) {
        this.stream = stream;
    }

    public byte getLocal() {
        return local;
    }

    public void setLocal(byte local) {
        this.local = local;
    }

    public byte getBackType() {
        return backType;
    }

    public void setBackType(byte backType) {
        this.backType = backType;
    }

    public byte getMultiple() {
        return multiple;
    }

    public void setMultiple(byte multiple) {
        this.multiple = multiple;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buffer = new MyBuffer();
        buffer.put(getServerIpLength());
        buffer.put(getServerIp());
        buffer.put(getServerTcpPort());
        buffer.put(getServerUdpPort());
        buffer.put(getChannel());
        buffer.put(getType());
        buffer.put(getStream());
        buffer.put(getLocal());
        buffer.put(getBackType());
        buffer.put(getMultiple());

        //设置起始时间
        buffer.put(Byte.parseByte(getStartTime().substring(2, 4), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(5, 7), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(8, 10), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(11, 13), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(14, 16), 16));
        buffer.put(Byte.parseByte(getStartTime().substring(17, 19), 16));
        //设置终止时间
        buffer.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(5, 7), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(8, 10), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(11, 13), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(14, 16), 16));
        buffer.put(Byte.parseByte(getEndTime().substring(17, 19), 16));

        return buffer.array();
    }

    @Override
    public void ReadFromBytes(byte[] body) {
        MyBuffer buffer = new MyBuffer(body);
        setServerIpLength(buffer.get());
        int length = BitConverter.ToUInt32(getServerIpLength());
        setServerIp(buffer.getString(length));
        setServerTcpPort(buffer.getShort());
        setServerUdpPort(buffer.getShort());
        setChannel(buffer.get());
        setType(buffer.get());
        setStream(buffer.get());
        setLocal(buffer.get());
        setBackType(buffer.get());
        setMultiple(buffer.get());
        byte[] start = buffer.gets(6);
        setStartTime("20" + String.format("%02X", start[0]) + "-"
                + String.format("%02X", start[1]) + "-"
                + String.format("%02X", start[2]) + " "
                + String.format("%02X", start[3]) + ":"
                + String.format("%02X", start[4]) + ":"
                + String.format("%02X", start[5]));
        byte[] end = buffer.gets(6);
        setEndTime("20" + String.format("%02X", end[0]) + "-"
                + String.format("%02X", end[1]) + "-"
                + String.format("%02X", end[2]) + " "
                + String.format("%02X", end[3]) + ":"
                + String.format("%02X", end[4]) + ":"
                + String.format("%02X", end[5]));
    }
}
